package navigation

import (
	"time"

	"BookStack/pkg/errors"
	"BookStack/pkg/utils"
)

// 导航状态常量
const (
	StatusDisabled = 0 // 禁用
	StatusEnabled  = 1 // 启用
)

// 常用导航位置
const (
	PositionHeader  = "header"  // 头部导航
	PositionFooter  = "footer"  // 底部导航
	PositionSidebar = "sidebar" // 侧边栏导航
	PositionMobile  = "mobile"  // 移动端导航
)

// Navigation 导航实体
type Navigation struct {
	ID          uint      `json:"id" gorm:"primaryKey;column:id"`
	Name        string    `json:"name" gorm:"column:name;size:50;not null"`              // 导航名称
	URL         string    `json:"url" gorm:"column:url;size:255;not null"`               // 导航链接
	Icon        string    `json:"icon" gorm:"column:icon;size:50"`                       // 导航图标
	Position    string    `json:"position" gorm:"column:position;size:20;not null"`      // 导航位置
	Order       int       `json:"order" gorm:"column:order;default:0"`                   // 排序序号
	Status      int       `json:"status" gorm:"column:status;default:1;comment:0禁用,1启用"` // 状态
	Target      string    `json:"target" gorm:"column:target;size:20;default:_self"`     // 打开方式
	Description string    `json:"description" gorm:"column:description;size:200"`        // 描述
	ParentID    *uint     `json:"parent_id" gorm:"column:parent_id;default:null"`        // 父级导航ID
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at;autoCreateTime"`
	UpdatedAt   time.Time `json:"updated_at" gorm:"column:updated_at;autoUpdateTime"`
}

// TableName 指定表名
func (Navigation) TableName() string {
	return "navigations"
}

// NewNavigation 创建一个新的导航
func NewNavigation(name, url, position string, parentID *uint, order int, status int, icon, target, description string) (*Navigation, error) {
	if name == "" {
		return nil, errors.New(errors.ErrCodeNavigationNameRequired, "导航名称不能为空")
	}
	if url == "" {
		return nil, errors.New(errors.ErrCodeNavigationURLRequired, "导航URL不能为空")
	}
	if position == "" {
		return nil, errors.New(errors.ErrCodeNavigationPositionRequired, "导航位置不能为空")
	}

	now := time.Now()
	return &Navigation{
		Name:        name,
		URL:         url,
		Icon:        icon,
		Position:    position,
		ParentID:    parentID,
		Order:       order,
		Status:      status,
		Target:      target,
		Description: description,
		CreatedAt:   now,
		UpdatedAt:   now,
	}, nil
}

// Update 更新导航
func (n *Navigation) Update(name, url, position string, parentID *uint, order int, status int, icon, target, description string) error {
	if name == "" {
		return errors.New(errors.ErrCodeNavigationNameRequired, "导航名称不能为空")
	}
	if url == "" {
		return errors.New(errors.ErrCodeNavigationURLRequired, "导航URL不能为空")
	}
	if position == "" {
		return errors.New(errors.ErrCodeNavigationPositionRequired, "导航位置不能为空")
	}

	n.Name = name
	n.URL = url
	n.Icon = icon
	n.Position = position
	n.ParentID = parentID
	n.Order = order
	n.Status = status
	n.Target = target
	n.Description = description
	n.UpdatedAt = time.Now()
	return nil
}

// SetStatus 设置导航状态
func (n *Navigation) SetStatus(status int) error {
	if status != StatusEnabled && status != StatusDisabled {
		return errors.New(errors.ErrCodeNavigationStatusInvalid, "导航状态无效，只能是0或1")
	}

	n.Status = status
	n.UpdatedAt = time.Now()
	return nil
}

// NavigationNode 树形结构的导航节点
type NavigationNode struct {
	ID          uint              `json:"id"`
	Name        string            `json:"name"`
	URL         string            `json:"url"`
	Icon        string            `json:"icon"`
	Position    string            `json:"position"`
	Order       int               `json:"order"`
	Status      int               `json:"status"`
	Target      string            `json:"target"`
	Description string            `json:"description"`
	Children    []*NavigationNode `json:"children"`
	CreatedAt   utils.JSONTime    `json:"created_at"`
	UpdatedAt   utils.JSONTime    `json:"updated_at"`
}

// OrderItem 排序项
type OrderItem struct {
	ID    uint `json:"id"`
	Order int  `json:"order"`
}

// CreateNavigationParams 创建导航参数
type CreateNavigationParams struct {
	Name        string `json:"name"`
	URL         string `json:"url"`
	Icon        string `json:"icon"`
	Position    string `json:"position"`
	ParentID    uint   `json:"parent_id"`
	Order       int    `json:"order"`
	Status      int    `json:"status"`
	Target      string `json:"target"`
	Description string `json:"description"`
}

// UpdateNavigationParams 更新导航参数
type UpdateNavigationParams struct {
	ID          uint   `json:"id"`
	Name        string `json:"name"`
	URL         string `json:"url"`
	Icon        string `json:"icon"`
	Position    string `json:"position"`
	ParentID    uint   `json:"parent_id"`
	Order       int    `json:"order"`
	Status      int    `json:"status"`
	Target      string `json:"target"`
	Description string `json:"description"`
}

// IsActive 检查导航是否激活
func (n Navigation) IsActive() bool {
	return n.Status == StatusEnabled
}

// HasParent 检查是否有父级导航
func (n Navigation) HasParent() bool {
	return n.ParentID != nil && *n.ParentID > 0
}
